<template>
  <div class="app-container" v-enter-helper="getList">
    <el-card>
      <div slot="header">
        <el-row>
          <!-- <el-form
            :inline="true"
            :model="listQuery"
            ref="dataList2"
            class="demo-form-inline"
          >
            <el-form-item label="">
              <el-select v-model="listQuery.type" placeholder="分类" clearable>
                <el-option
                  @clear="listQuery.type = undefined"
                  v-for="item in TypeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="">
              <el-input
                v-model.trim="listQuery.content"
                placeholder="内容"
                clearable
              ></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form> -->
        </el-row>
        <el-button type="primary" @click="handleAdd">新增</el-button>
        <el-button type="danger" @click="handleRefuseAll">一键删除</el-button>
      </div>
      <div>
        <el-table
          :data="recordData"
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="序号"></el-table-column>
          <el-table-column prop="content" label="文案内容"></el-table-column>
          <el-table-column prop="type" label="分类">
            <template slot-scope="scope">
              <div v-if="scope.row.type === 2">用户</div>
              <div v-if="scope.row.type === 1">主播</div>
            </template>
          </el-table-column>
          <el-table-column prop="sort" label="默认排序"></el-table-column>
          <el-table-column prop="is_default" label="默认展示">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.is_default">是</el-tag>
              <el-tag type="warning" v-else>否</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="handleEdit(scope.row)"
                plain
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                plain
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.page_size"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 新增编辑 -->
    <el-dialog
      :title="titleOptions[title] + '常用语'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="30%"
      @close="handleClear"
      :before-close="handleClose"
    >
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item label="文案内容" prop="content" label-width="120px">
          <el-input
            clearable
            placeholder="请输入文案类型"
            v-model="form.content"
            style="width:270px"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type" label-width="120px">
          <el-select
            v-model="form.type"
            placeholder="请选择"
            style="width:270px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序号" prop="sort" label-width="120px">
          <el-input
            type="number"
            clearable
            placeholder="请输入排序号"
            style="width:270px"
            v-model.number="form.sort"
          ></el-input>
        </el-form-item>
        <el-form-item label="默认展示" prop="is_default" label-width="120px">
          <el-radio v-model="form.is_default" :label="1">是</el-radio>
          <el-radio v-model="form.is_default" :label="0">否</el-radio>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" plain @click="handleClose">返回</el-button>
        <el-button type="primary" @click="deBounceWrap(handleSumbit, 'form')"
          >提交</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  commonList,
  addCommon,
  editCommon,
  delCommonList
} from "@/api/room/roomCommonWords";
export default {
  data() {
    return {
      TypeOptions: [
        { value: 1, label: "主播" },
        { value: 2, label: "用户" }
      ],

      recordData: [],
      dialogVisible: false,
      titleOptions: {
        add: "新增",
        edit: "编辑"
      },
      title: "add",
      rowData: {},
      listQuery: {
        page: 1,
        page_size: 10,
        content: "",
        type: undefined
      },
      total: 0,
      form: {
        content: undefined,
        type: undefined,
        sort: undefined,
        is_default: 0
      },

      rules: {
        content: [
          { required: true, message: "请输入内容", trigger: "blur" },
          { max: 12, message: "不能超过12个字符", trigger: "blur" }
        ],
        type: [{ required: true, message: "请选择类型", trigger: "change" }],
        is_default: [
          { required: true, message: "请选择是否默认展示", trigger: "change" }
        ]
      },
      options: [
        {
          value: 1,
          label: "主播"
        },
        {
          value: 2,
          label: "用户"
        }
      ],
      //勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    searchSubmit() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetSubmit() {
      this.listQuery.page = 1;
      this.listQuery.perPage = 10;
      this.listQuery.content = "";
      this.listQuery.type = undefined;
      this.getList();
    },
    getList() {
      commonList(this.listQuery).then(res => {
        this.recordData = res.data.data;
        this.total = res.data.total;
        // this.listQuery.page = res.data.current_page;
        // this.listQuery.page_size = res.data.per_page;
      });
    },
    handleAdd() {
      this.title = "add";
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.title = "edit";
      this.rowData = row;
      this.form.content = this.rowData.content;
      this.form.sort = this.rowData.sort;
      this.form.type = this.rowData.type;
      this.form.id = this.rowData.id;
      this.form.is_default = this.rowData.is_default;
      this.dialogVisible = true;
    },
    handleSumbit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title === "add") {
            addCommon(this.form).then(res => {
              if (res.code === 200) {
                this.$message.success("操作成功");
                this.getList();
                this.dialogVisible = false;
              } else {
              }
            });
          }
          if (this.title === "edit") {
            editCommon(this.form).then(res => {
              if (res.code === 200) {
                this.$message.success("编辑成功");
                this.getList();
                this.dialogVisible = false;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    handleClear() {
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
      this.form = {
        content: undefined,
        type: undefined,
        sort: undefined,
        is_default: 0
      };
    },
    handleDelete(row) {
      this.$confirm("确认删除!?此步骤无法恢复。", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let data = {
            ids: row.id
          };
          delCommonList(data).then(res => {
            if (res.code === 200) {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSelectionChange(giftList) {
      this.multipleSelection = giftList;
    },
    handleRefuseAll() {
      if (this.multipleSelection.length) {
        /* 存在勾选数据 */ const ids = this.multipleSelection.reduce(
          (pre, currRow) => {
            pre += `${currRow.id},`;
            return pre;
          },
          ""
        );
        delCommonList({ boss_pool_type: 6, ids })
          .then(() => this.$message.success("删除成功！"))
          .catch(console.error)
          .finally(() => /* 刷新数据 */ {
            this.multipleSelection = [];
            this.getList();
          });
      } /* 不存在勾选数据 */ else {
        this.$message.warning("请勾选要删除的数据！");
      }
    }
  }
};
</script>

<style></style>
